<template>
  <!-- 质量整改通知单业务待办 -->
  <!-- <base-breadcrumb :title="$route.meta.title"> -->
  <div>
    <a-spin :spinning="loading">
      <!-- 问题信息 -->
      <a-card :bordered="false">
        <title-name title="处理"></title-name>
        <div class="ml12">
          <!-- 风险处理方案信息 -->
          <RiskTreatmentPlanInfo ref="RiskTreatmentPlanInfo" :form="riskTrackVO" :onlyRead="resolveInfo" />
          <!-- 风险跟踪计划信息 -->
          <RiskTrackingPlanInfo ref="RiskTrackingPlanInfo" :form="riskTrackVO" :onlyRead="resolveInfo" />
          <!-- 操作栏 -->
          <div class="btn_box" align="right" style="margin-top: 20px;">
            <a-button type="primary" :loading="auditLoading" class="ml12" @click="handleSubmit()" v-if="!resolveInfo">确认处理</a-button>
          </div>
        </div>
      </a-card>
      <!-- 业务详情 -->
      <a-card :bordered="false">
        <h1>业务详情</h1>
        <!-- 基本信息 -->
        <BaseInfo ref="BaseInfo" :form="detailForm" :onlyRead="true" :orgTreeDisabled="false" />
        <!-- 风险上报信息 -->
        <RiskReportingInfo ref="RiskReportingInfo" :form="detailForm" :onlyRead="true" />
        <!-- 风险复核信息 -->
        <RiskReviewInfo ref="RiskReviewInfo" :form="detailForm" :onlyRead="true" />
      </a-card>
    </a-spin>
    <div class="btn_box tool-btns--fixed page-btn-right-top">
      <a-button type="primary" @click="$close">关闭</a-button>
    </div>
    </div>
  <!-- </base-breadcrumb> -->
</template>

<script>
  import RiskTreatmentPlanInfo from '@/views/quality/riskIdentification/components/RiskTreatmentPlanInfo'
  import RiskTrackingPlanInfo from '@/views/quality/riskIdentification/components/RiskTrackingPlanInfo'
import BaseInfo from '@/views/quality/riskIdentification/components/BaseInfo'
import RiskReportingInfo from '@/views/quality/riskIdentification/components/RiskReportingInfo'
import RiskReviewInfo from '@/views/quality/riskIdentification/components/RiskReviewInfo'
import { riskTracking } from '@/api/quality/risk/riskTracking'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
const columns1 = [
  {
    title: '损失类型',
    dataIndex: 'treeName',
    width: 180,
  },
  {
    title: '损失描述',
    dataIndex: 'description',
    ellipsis: true,
  },
  {
    title: '补充描述',
    dataIndex: 'supplementDescription',
    scopedSlots: { customRender: 'supplementDescription' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 100,
    scopedSlots: { customRender: 'action' },
  }
]
const columns2 = [
  {
    title: '损失类型',
    dataIndex: 'treeName',
    width: 180,
  },
  {
    title: '损失描述',
    dataIndex: 'description',
    ellipsis: true,
  },
  {
    title: '补充描述',
    dataIndex: 'supplementDescription',
  },
]
export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    BaseInfo,
    RiskReportingInfo,
    RiskTreatmentPlanInfo,
    RiskTrackingPlanInfo,
    RiskReviewInfo,
  },
  data() {
    return {
      bussinessId: this.$route.query.bussinessId || '',
      escalationId: this.$route.query.escalationId || '',
      auditLoading: false,
      loading: false, // 请求加载框
      detailForm: {
        projectId:"",
        updateUserName: this.$userInfo().name,
        fillTime: this.$dateFormat(Date.now(), 'D'),
      },
      riskTrackVO: {},
      form: {
        projectId: ''
      },
      api: riskTracking, // 请求api
      visible: false,
      dataSource: [],
      dataSourceReview: [],
      outSelectedRowKeys: [],
      columns1,
      columns2,
    }
  },
  created() {
    // 请求业务详情
    this.init()
    document.title = '中建八局'
  },
  computed: {
     // 是否是业务待办查看处理信息
     resolveInfo: {
      get() {
        return this.$route.query?.resolveInfo === 'true'
      },
      set() {}
    },
  },
  methods: {
    /**
     * @description: 请求业务详情
     */
    init() {
      if (this.escalationId) {
        this.loading = true
        this.api
          .businessToDo(this.escalationId)
          .then((res) => {
            if (res.data) {
              // 映射表单
              res.data.projectTypeId = res.data.projectTypeId ? res.data.projectTypeId.toString(): ''
              res.data.subProjectId = res.data.subProjectId ? res.data.subProjectId.toString(): ''
              this.riskTrackVO = res.data.riskTrackVO
              this.detailForm = res.data
              this.form.projectId = res.data.projectId
              this.detailForm.viewName = res.data.branchComName + '/' + res.data.projectName
            }
          })
          .finally(() => {
            this.loading = false
          })
      }
    },
    /**
     * @description: 处理
     * @return {*}
     */
    handleSubmit() {
      try {
          const RiskTreatmentPlanInfo = this.$refs.RiskTreatmentPlanInfo
          RiskTreatmentPlanInfo.$refs?.ruleForm.validate(valid => {
          if (valid) {
            const RiskTrackingPlanInfo = this.$refs.RiskTrackingPlanInfo
            const columns1 = RiskTrackingPlanInfo.columns || []
            const columns = columns1.filter(v => v.dataIndex !== 'action').map(item => item.dataIndex)
            const dataSource = RiskTrackingPlanInfo.dataSource || []
            if (dataSource.length === 0) {
              this.$warning({
                title: '提示',
                content: '请添加风险跟踪计划信息',
                okText: '确定'
              })
              return false
            }
            const emtyArr = dataSource.filter((obj) => {
              let tag = false
              for (let key in obj) { 
                if (columns.indexOf(key) !== -1 && !obj[key]) {
                  tag = true
                  break;
                }
              }
              return tag;
            })
            if (emtyArr.length) {
              this.$warning({
                title: '提示',
                content: '请将风险跟踪计划信息填写完整再提交！',
                okText: '确定'
              })
              return false
            }
            const takeEffectDate = dataSource.filter(item => this.moment(item['takeEffectDate']).diff(this.moment(new Date()), 'days') < 0 && item.operation != false)
            if (takeEffectDate.length) {
              this.$warning({
                title: '提示',
                content: '生效日期不能小于今天',
                okText: '确定'
              })
              return false
            }
            this.$confirm({
              titile: '提示',
              content: '确认提交，提交后不可撤回？',
              onOk: () => {
                const submitData = {
                  status: 1,
                  handleOpinion: RiskTreatmentPlanInfo.form.handleOpinion,
                  filesIds: RiskTreatmentPlanInfo.form.fileInfos ? RiskTreatmentPlanInfo.form.fileInfos.map(item => item.commonId) : [],
                  details: dataSource,
                  id: this.escalationId,
                  noticeId: this.bussinessId,
                }
                this.auditLoading = true
                this.api['updatePost'](submitData)
                  .then((res) => {
                    if (res.code === 200 || res.data.code === 200) {
                      this.$message.success('提交成功！')
                      this.$multiTab.close()
                    }
                  })
                  .finally(() => {
                    this.auditLoading = false
                  })
              }
            })
          }
        })
      } catch (e) {
        console.log(e)
      }
    },
  }
}
</script>